
local function target_hit_fn(target)
	if target and  target:IsValid() and target.components.health then
		SpawnPrefab("collapse_big").Transform:SetPosition(target.Transform:GetWorldPosition())  --- 烟雾
		target.components.health:Kill()
	end
end

local function target_and_point_check(inst,monster_insts)
	-- 1 金 100 分，小怪 25 分， BOSS 4000 分 -- 只杀攻击营地的两个 bearger 和 deerclops
	local monster_point_num = 25
	local boss_point_num = 4000

	-------- 先分离BOSS列表 和 非BOSS列表
	local BOSS_NAME_LIST = {
		["bearger"] = true,
		["deerclops"] = true,
	}

	local need_2_kill_monsters_list = {}

	for k, temp_monster in pairs(monster_insts) do
		if temp_monster then
			if not temp_monster:HasTag("epic") then	--- 如果是非boss
				local combat_com = temp_monster.components.combat
				if combat_com and combat_com.target and 
				 ( combat_com.target:HasTag("player") or combat_com.target:HasTag("companion") or combat_com.target:HasTag("npc") ) 
				 or temp_monster.prefab == "bat"
				 then
					need_2_kill_monsters_list[temp_monster] = monster_point_num
				 end
			else	-------- 如果是BOSS
				if BOSS_NAME_LIST[temp_monster.prefab] == true then
					need_2_kill_monsters_list[temp_monster] = boss_point_num
				end
			end
		end
	end

	local ret_targets_list = {}

	for temp_monster, cost_point in pairs(need_2_kill_monsters_list) do
		local current_point = inst.npc_everything_data:Add("goldnugget.point",0)
		if current_point >= cost_point then
			table.insert(ret_targets_list,temp_monster)
			inst.npc_everything_data:Add("goldnugget.point",-cost_point)
		end
	end


	return ret_targets_list
end
local function Vigilance_Task(inst)
	if inst.npc_everything_data:Add("goldnugget.point",0) <= 0 then
		return
	end

	local x,y,z = inst.Transform:GetWorldPosition()
	local canthavetags = { "npc_task","clock_work_kill_target","companion","isdead","player","INLIMBO", "notarget", "noattack", "flight", "invisible", "playerghost" ,"chester","hutch","wall","structure"}
	local musthavetags = nil
	local musthaveoneoftags = {"pig","rabbit","animal","smallcreature","epic","monster","insect","npc_tag.lunarthrall_plant"}
	local monster_insts = TheSim:FindEntities(x, 0, z, 40, musthavetags, canthavetags, musthaveoneoftags)
	if #monster_insts == 0 then
		return
	end
	---------------------------------------------------------------


	local target_list = target_and_point_check(inst,monster_insts)

	---------------------------------------------------------------
	if #target_list == 0 then
		return
	end

	for k, monster in pairs(target_list) do
		monster:AddTag("clock_work_kill_target")
	end

	local eggs = inst.npc_base_lib:Throw_Out_Items({
		target = inst,
		name = "npc_item_clockwork_egg_item",
		range = 8,
		height = 10,
		random_height = true,
		num = #target_list
		
	})

	for i, temp_egg in ipairs(eggs) do
		temp_egg:PushEvent("Set",{
			target = target_list[i],
			owner = inst,
			target_fn = target_hit_fn,
		})
	end


end
---------------------------------------------------------------------------------------------------------
local function Player_Close_In_Tree_task(inst)
	if inst.components.container and inst.components.container:IsOpen() then
		inst.npc_base_lib:Hide_Chat_Bubble()
		return
	end

	if inst:IsNearPlayer(4) == false then
		inst.npc_base_lib:Hide_Chat_Bubble()
		return
	end

	if inst.npc_everything_data:Add("goldnugget.point",0) <= 0 then
		inst.npc_base_lib:Show_Chat_Bubble({
			image = "goldnugget",
			height = 6.5,
		})
	else
		inst.npc_base_lib:Hide_Chat_Bubble()
		inst.npc_base_lib:Say(inst.npc_everything_data:Get("goldnugget.point"))

	end


end
---------------------------------------------------------------------------------------------------------
-------- 弄个小东西在附近跑
local function clockwork_pet_init(inst)
	local x,y,z = inst.Transform:GetWorldPosition()

	local pet_target = SpawnPrefab("npc_item_clockwork_egg_pet_target")
	pet_target.__tree = inst
	pet_target.Ready = true
	inst.npc_base_lib:Add2Child(pet_target)
	pet_target:PushEvent("new_location")

	local clockwork_pet = SpawnPrefab("npc_item_clockwork_egg_pet")
	inst.npc_base_lib:Add2Child(clockwork_pet)
	clockwork_pet.Transform:SetPosition(x,y,z)
	clockwork_pet.___pet_target = pet_target
	clockwork_pet.Ready = true

end
---------------------------------------------------------------------------------------------------------
local function fn(inst)
	

	inst:ListenForEvent("onclose",function()
		local COM_Container = inst.components.container
		local flg,num = COM_Container:Has("goldnugget",0)
		if num > 0 then
			---- 1 金 100分
			inst.npc_everything_data:Add("goldnugget.point",num*100)
			local slot_clear_fn = function(item)
				if item and item.prefab == "goldnugget" then
					item:Remove()
				end
			end
			inst.components.container:ForEachItem(slot_clear_fn)
			inst.components.container:DropEverything()
			

		end
		inst.npc_base_lib:Say( inst.npc_everything_data:Add("goldnugget.point",0) )
	end)


	inst:ListenForEvent("Return_Back_Gold",function()
		local gold_points = inst.npc_everything_data:Add("goldnugget.point",0) /2
		local gold_num = math.floor(gold_points/100)
		if gold_num > 0 then
			local max_stack_check_inst = SpawnPrefab("goldnugget")
			local max_stack_num = max_stack_check_inst.components.stackable.maxsize
			max_stack_check_inst:Remove()

			local rest_num = gold_num % max_stack_num
			local grup_num = math.ceil(  (gold_num - rest_num) / max_stack_num   )
			if grup_num > 0 then
				local t_golds = inst.npc_base_lib:Throw_Out_Items({
					target = inst ,
					name = "goldnugget",
					range = 4,
					height = 6,
					num = grup_num
				})
				for k, v in pairs(t_golds) do
					v.components.stackable.stacksize = max_stack_num
				end
			end

			if rest_num > 0 then
				local rest_t_golds = inst.npc_base_lib:Throw_Out_Items({
					target = inst ,
					name = "goldnugget",
					range = 4,
					height = 6,
					num = 1
				})
				rest_t_golds[1].components.stackable.stacksize = rest_num
			end
		end
	end)


	inst:DoPeriodicTask(3,function()
		Vigilance_Task(inst)
		Player_Close_In_Tree_task(inst)

		if inst.npc_everything_data:Add("goldnugget.point",0) > 0 then
			inst:PushEvent("turn_on")
		else
			inst:PushEvent("turn_off")
		end
	end)

	-- inst:DoPeriodicTask(2,function()
	-- 	Player_Close_In_Tree_task(inst)
	-- end)

	inst:DoTaskInTime(3,clockwork_pet_init)

end

local function Fn()
    local Fx = Class()

    function Fx:Init(inst)
        local ret = fn(inst)
        return ret
    end


    return Fx
end

return Fn